package com.zs.framework.db.multiple.annotation;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * 用于实现多数据源切换的AOP类，需要在方法上使用@SpecDatasource注解来启用
 * @author ligq
 */
@Component
@Aspect
public class MethodSwitchDatasourceAspectJ implements Ordered {

    @Pointcut("@annotation(com.zs.framework.db.multiple.annotation.SpecDatasource)")
    public void pointcut() {
    }

    @Before(value="pointcut() && @annotation(specDatasource)", argNames="specDatasource")
    public void before(SpecDatasource specDatasource){
        DynamicDataSource.setDataSourceKey(specDatasource.value());
    }
    @After("pointcut()")
    public void after() {
        DynamicDataSource.cleanDataSourceKey();
    }

    @Override
    public int getOrder() {
        return 2563336;
    }
}
